﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_KCYJ : Form
    {
        SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte
        string xlgl;
        //DataTable dt = new DataTable();
        public YP_KCYJ()
        {
            InitializeComponent();
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((ActiveControl is TextBox || ActiveControl is Button) && keyData == Keys.Enter)
                keyData = Keys.Tab;
            return base.ProcessDialogKey(keyData);
        }

        private void KC_show()
        {
            if(radioButton1.Checked==true)//不限
                xlgl="";
            if (radioButton2.Checked == true)//正常
                xlgl = " AND (KCSL BETWEEN XX AND SX)";
            if (radioButton3.Checked == true)//高限
                xlgl = " AND KCSL>SX";
            if (radioButton4.Checked == true)//低限
                xlgl = "  AND KCSL<XX";
            
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    DataTable dt_bmdw = new DataTable();
                    SqlCommand BMDW = new SqlCommand("SELECT ZD_YP.BM 编码,MC 名称,GG 规格,SX 上限,XX 下限,KCSL 库存数量,DH FROM ZD_YP,(SELECT YPBM ,SUM(SL) AS KCSL FROM YP_CRKJZ GROUP BY YPBM) YPKC WHERE ZD_YP.BM=YPBM AND (BM LIKE'%" + textBoxX1.Text.ToString() + "%' OR DH LIKE'%" + textBoxX1.Text.ToString() + "%' OR MC LIKE'%" + textBoxX1.Text.ToString() + "%')"+xlgl, con);//sql语句
                    da.SelectCommand = BMDW;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_bmdw);
                    dataGridViewX1.DataSource = dt_bmdw;
                    dataGridViewX1.Columns[6].Visible = false;
                    System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
                    dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    dataGridViewX1.Columns[5].DefaultCellStyle = dataGridViewCellStyle2;
                    for (int i = 0; i < dataGridViewX1.RowCount-1; i++)
                    {
                        if (dataGridViewX1.Rows[i].Cells[3].Value.ToString() != "" && dataGridViewX1.Rows[i].Cells[4].Value.ToString() != "")
                        {
                            if (Convert.ToDouble(dataGridViewX1.Rows[i].Cells[5].Value) > Convert.ToDouble(dataGridViewX1.Rows[i].Cells[3].Value))
                                dataGridViewX1.Rows[i].Cells[5].Style.ForeColor = System.Drawing.Color.Green;

                            if (Convert.ToDouble(dataGridViewX1.Rows[i].Cells[5].Value) < Convert.ToDouble(dataGridViewX1.Rows[i].Cells[4].Value))
                                dataGridViewX1.Rows[i].Cells[5].Style.ForeColor = System.Drawing.Color.Red;
                            if (Convert.ToDouble(dataGridViewX1.Rows[i].Cells[5].Value) < Convert.ToDouble(dataGridViewX1.Rows[i].Cells[3].Value) && Convert.ToDouble(dataGridViewX1.Rows[i].Cells[5].Value) > Convert.ToDouble(dataGridViewX1.Rows[i].Cells[4].Value))
                                dataGridViewX1.Rows[i].Cells[5].Style.ForeColor = System.Drawing.Color.Blue;
                        }
                    }

                }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.ToString());
            }
 
        }
        private void YP_KCYJ_Load(object sender, EventArgs e)
        {
            KC_show();
            txt_BM.Enabled = false;
            txt_MC.Enabled = false;
            txt_GG.Enabled = false;
        }

        private void XLGL_ALL_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {
            KC_show();
        }

        private void btn_JSKC_Click(object sender, EventArgs e)
        {

            KC_show();
        }

        private void dataGridViewX1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                txt_BM.Text = dataGridViewX1.CurrentRow.Cells[0].Value.ToString();
                txt_MC.Text = dataGridViewX1.CurrentRow.Cells[1].Value.ToString();
                txt_GG.Text = dataGridViewX1.CurrentRow.Cells[2].Value.ToString();
                txt_SX.Text = dataGridViewX1.CurrentRow.Cells[3].Value.ToString();
                txt_XX.Text = dataGridViewX1.CurrentRow.Cells[4].Value.ToString();
            }
            catch { }
        }


        private void buttonX1_Click_1(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con_bc = new SqlConnection(SQL.connectDB);
                con_bc.Open();

                SqlCommand sc_bc = new SqlCommand("UPDATE ZD_YP SET SX=" + Convert.ToInt16(txt_SX.Text) + ",XX=" + Convert.ToInt16(txt_XX.Text) + " WHERE BM='" + txt_BM.Text.ToString() + "'", con_bc);//sql语句
                sc_bc.ExecuteNonQuery();
                dataGridViewX1.CurrentRow.Cells[3].Value = txt_SX.Text.ToString();
                dataGridViewX1.CurrentRow.Cells[4].Value = txt_XX.Text.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            KC_show();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            KC_show();
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            KC_show();
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            KC_show();
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            dgVprint1.Print(dataGridViewX1, false);
        }
    }
}